package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileTools {
	/**
	 * 
	 * 鎶婂瓧绗︽祦鐨勫啓鍏ユ枃浠跺熬閮� 棣栧厛鍒ゆ柇鍐欏叆鐨勬枃浠舵槸鍚﹀瓨鍦紝濡傛灉瀛樺湪锛屽厛璇诲彇鏂囦欢鍐呭锛岀劧鍚庡拰鏂板瓧绗︿覆涓�璧峰啓鍏ワ紝 濡傛灉涓嶅瓨鍦紝鍏堝垱寤烘枃浠讹紝鐒跺悗鍐欏叆鏂囦欢銆�
	 * 
	 * @param filePath
	 *            鏂囦欢璺緞
	 * @param str
	 *            瑕佸啓鍏ョ殑瀛楃
	 * 
	 * @return
	 */
	public static void writeStrToEnd(String filePath, String str) {
		BufferedReader br = null;
		String lineStr = "";
		StringBuffer sb = new StringBuffer();

		BufferedWriter bw = null;
		try {
			File file = new File(filePath);
			if (!file.exists()) {
				file.createNewFile();
			}

			br = new BufferedReader(new FileReader(filePath));
			while ((lineStr = br.readLine()) != null) {
				sb.append(lineStr);
				sb.append("\r\n");
			}
			bw = new BufferedWriter(new FileWriter(filePath));
			bw.write(sb.toString() + str);
			bw.flush();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		} finally {
			try {
				bw.close();
				br.close();
			} catch (IOException e3) {
				e3.printStackTrace();
			}
		}
	}

	public static void FileOutputStream(String filepath,String str) throws Exception {

		 /*File txt=new File(filepath);
		 if(!txt.exists()){
			 txt.createNewFile();
		 }
		 byte bytes[]=new byte[512];
		 bytes=str.getBytes(); //
		 int b=str.length(); //鏀�
		 FileOutputStream( fos=new FileOutputStream(txt);
		 fos.write(bytes,0,b);
		 fos.close();*/

		//String path = "D://Documents and Settings//bizOrdeId.txt";
		try {
			FileWriter fw = new FileWriter(filepath, true);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.newLine();
			bw.write(str);
			bw.close();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
